﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///YHDR_CDSX_KJDH 的摘要说明
/// </summary>
public  partial class YHDR_CDSX_KJDH
{
    public static YHDR_CDSX_KJDH GetByAddUserID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.YHDR_CDSX_KJDH.FirstOrDefault(row => row.UserID == id);
        }
    }

    public static List<YHDR_CDSX_KJDH> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.YHDR_CDSX_KJDH.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.YHDR_CDSX_KJDH.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<YHDR_CDSX_KJDH>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.YHDR_CDSX_KJDH.FirstOrDefault(row => row.UserID == id);
            if (objItem == null)
                return;
            context.YHDR_CDSX_KJDH.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<YHDR_CDSX_KJDH> GetAllByConditon(int userID, string AddTimeForm, string AddTimeTo)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.YHDR_CDSX_KJDH
                        select t;
            if (userID != 0)
                query = query.Where(row => row.UserID == userID);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.LastLoginTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.LastLoginTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            query = query.OrderByDescending(row => row.LastLoginTime);
            return query.ToList();
        }
    }
}